package com_.dxy_test_.spel_;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.PropertyAccessor;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class SpELTest2 {
    private static final ExpressionParser parser = new SpelExpressionParser();
    private static final ConcurrentHashMap<String, Expression> expressionMap = new ConcurrentHashMap<>();
    private static final List<PropertyAccessor> propertyAccessors = Collections.singletonList(new MapAccessor());

    private static Expression getExpression(String template) {
        Expression expression = expressionMap.get(template);
        if (expression != null) {
            return expression;
        } else {
            Expression _expression = parser.parseExpression(template, new TemplateParserContext());
            expressionMap.put(template, _expression);
            return _expression;
        }
    }

    public static String test(String template, String jsonObject){
        Expression expression = getExpression(template);
        StandardEvaluationContext context = new StandardEvaluationContext(JSON.parseObject(jsonObject));
        context.setPropertyAccessors(propertyAccessors);
        return (String) expression.getValue(context);
    }

    public static void main(String[] args) {
        String template = "{\"touser\":\"#{openid}\",\"template_id\":\"nOijOQYeQXTGUW-fSBD8TM2OzmZr8ksO46KHWLmFvwo\",\"url\":\"#{url}\",\"miniprogram\":{\"appid\":\"#{appid}\",\"pagepath\":\"#{pagePath}\"},\"data\":{\"first\":{\"value\":\"您申请办理的签证失败，护照资料退回中\",\"color\":\"#173177\"},\"keyword1\":{\"value\":\"#{arg1}签证办理\",\"color\":\"#173177\"},\"keyword2\":{\"value\":\"护照退回，派送中\",\"color\":\"#173177\"},\"keyword3\":{\"value\":\"#{arg2}\",\"color\":\"#173177\"},\"remark\":{\"value\":\"点击查看订单详情及物流进度，如有疑问可联系客服18988900373\",\"color\":\"#173177\"}}}";
        JSONObject param = new JSONObject();
        param.put("openid","我是openid");
        param.put("url","我是url");
        param.put("appid","我是appid");
        param.put("pagePath","我是pagePath");
        param.put("arg1","我是arg1");
        param.put("arg2","我是arg2");

        String template1 = "#{content}|#{url}|#{wxurl}";
        JSONObject param1 = new JSONObject();
        param1.put("content","王二狗|13333333333");
        param1.put("url","http://www.xxx.com");
        param1.put("wxurl","http://xxxx");

        String template2 = "aaa#{test}";
        JSONObject param2 = new JSONObject();
        param2.put("test","vvv");
        param2.put("test2","www");

        System.out.println("template: "+test(template,param.toJSONString()));
        System.out.println("template1: "+test(template1,param1.toJSONString()));
        System.out.println("template2: "+test(template2,param2.toJSONString()));



    }
}
